package com.example.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: shenmengwei
 * @Description:
 * @Date: Created on 2018/6/14 15:49
 * @Modified By:
 */
public class DateUtil {



    private static  int  one =1;
    private static  int  three =3;
    private static  int  four =4;
    private static  int  six =6;
    private static  int  seven =7;
    private static  int  nine =9;
    /**
     * 长日期格式
     */
    private static String LONG_TIME = "yyyy-MM-dd HH:mm:ss";
    /**
     * 中间期格式
     */
    private static String MIDLE_TIME = "yyyy-MM-dd HH:mm";
    /**
     * 短日期格式
     */
    private static String SHORT_TIME = "yyyy-MM-dd";



    /**
     * 短日期格式（中文）
     */
    private static String SHORT_TIME_CHS = "yyyy年MM月dd日";

    /**
     * 一天多少秒
     */
    private static long ONE_DAY_SECONDS = 86400;

    /**
     * 将日期格式化成yyyy-MM-dd HH:mm:ss格式的字符串
     * @param date
     * @return
     */
    public static String formatLongtime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(LONG_TIME);
        return sdf.format(date);
    }

    /**
     *  获取下一个时间
     * @param date1 开始时间
     * @param secs  时间间隔  可以为负数
     * @return
     */
    public static Date addSeconds(Date date1, long secs) {
        return new Date(date1.getTime() + (secs * 1000));
    }

    /**
     *  计算时间间隔(秒)
     * @param endDate
     * @param nowDate
     * @return
     *    long nd = 1000 * 24 * 60 * 60;
     *    long nh = 1000 * 60 * 60;
     *    long nm = 1000 * 60;
     *    long ns = 1000;
     *    获得两个时间的毫秒时间差异
     */
    public static long getDatePoor(Date endDate, Date nowDate) {
        return  endDate.getTime() - nowDate.getTime();
    }

    /**
     * 获取当天时间的最后一分钟
     * @param time
     * @return
     */
    public static  String  getDayLastTime(Date time){

        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
        String day =  sdf.format(time);
        return day+" 23:59";
    }

    /**
     * 获取当天时间的第一分钟
     * @param time
     * @return
     */
    public static  String  getDayStartTime(Date time){
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
        String day =  sdf.format(time);
        return day+" 00:00";
    }

    /**
     *  String 转成 Date
     * @param time
     * @return
     */
     public static  Date  getDate(String time)throws Exception{
         SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
          return  sdf.parse(time);
    }



    /**
     * 获取本周时间的最后一份钟
     * @param time
     * @return
     */
    public static  String  getWeekLastTime(Date time) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
        Calendar cale = Calendar.getInstance();
        cale.setTime(time);
        cale.set(Calendar.DAY_OF_WEEK, 1);
        cale.set(Calendar.DATE, cale.get(Calendar.DATE) + 6);
        String day =  sdf.format(cale.getTime());
        return day+" 23:59";
    }

    /**
     * 获取本周时间的第一分钟
     * @param time
     * @return
     */
    public static  String  getWeekStartTime(Date time) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date date = cal.getTime();
        String day =  sdf.format(date);
        return day+" 00:00";
    }

    /**
     * 获取本月时间的最后一份钟
     * @param time
     * @return
     */
    public static  String  getMonthLastTime(Date time) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));
        String day =  sdf.format(cale.getTime());
        return day+" 23:59";
    }

    /**
     * 获取本月时间的第一分钟
     * @param time
     * @return
     */
    public static  String  getMonthStartTime(Date time) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH,1);
        String firstDay = sdf.format(cale.getTime());
        return firstDay+" 00:00";
    }

    /**
     * 获取当前季度
     */
    public static int getQuarter(Date time) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        int month = c.get(Calendar.MONTH) + 1;
        int quarter = 0;
        if (month >= one && month <= three) {
            quarter = 1;
        } else if (month >= four && month <= six) {
            quarter = 2;
        } else if (month >= seven && month <= nine) {
            quarter = 3;
        } else {
            quarter = 4;
        }
        return quarter ;
    }

    /**
     * 获取本季度时间的最后一份钟
     * @param time
     * @return
     */
    public static  String  getQuarterLastTime( Date  time ) throws Exception{

        int qarter = getQuarter(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(time);
        return getYear(qarter,year);
    }

    /**
     * 获取本季度时间的最后一份钟
     * @param time
     * @return
     */
    public static  String  getQuarterStartTime( Date  time ) throws Exception{

        int qarter = getQuarter(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(time);
        return getYear(qarter,year);
    }

    /**
     * 获取最后截至时间
     * @param qarter
     * @param year
     * @return
     */
    private static String getYear(int qarter,String year){
        switch (qarter)
        {
            case 1 :
                year= year+"-01-01 00:00";
                break;
            case 2 :
                year= year+"-04-01 00:00";
                break;
            case 3 :
                year= year+"-07-01 00:00";
                break;
            case 4 :
                year= year+"-10-01 00:00";
                break;
            default:
        }
        return  year;
    }


    /**
     * 获取本年度时间的最后一份钟
     * @param time
     * @return
     */
    public static  String  getYearLastTime( Date  time ) throws Exception{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(time);
        return year+"-12-31 23:59";
    }

    /**
     * 获取本年度时间的第一分钟
     * @param time
     * @return
     */
    public static  String  getYearStartTime( Date  time ) throws Exception{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(time);
        return year+"-01-01 00:00";
    }

    /**
     * 时间比较大小
     *
     * 1 大于  -1 小于
     * @param time1
     * @param time
     * @return
     */
    public static int  compareTime(String time1, String time) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(MIDLE_TIME);
        int  flag = 0;
        Date dt1 = sdf.parse(time1);
        Date dt2 = sdf.parse(time);
        if (dt1.getTime() > dt2.getTime()) {

            flag =1;
        }
        else if (dt1.getTime() < dt2.getTime()) {

            flag =-1;
        }
        return  flag;
    }



    /**
     * 时间比较大小
     *
     * 1 大于  -1 小于
     * @param time1
     * @param time
     * @return
     */
    public static int  compareTime(Date time1, String time) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(LONG_TIME);
        int  flag = 0;
        Date dt2 = sdf.parse(time);
        if (time1.getTime() > dt2.getTime()) {

            flag =1;
        }
        else if (time1.getTime() < dt2.getTime()) {

            flag =-1;
        }
        return  flag;
    }



    /**
     * 获取去年
     * @param time
     * @return
     * @throws ParseException
     */
    public static String getYesterday(String time) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(format.parse(time));
        cal.add(Calendar.DAY_OF_MONTH,-1);
        String yesterday = format.format(cal.getTime());
        return yesterday;
    }

    /**
     * 短时间格式化
     * @param time
     * @return
     */
    public  static String  getFromate(Date time){
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME);
        return sdf.format(time);
    }







    /**
     * 中间时间格式化
     * @param time
     * @return
     */
    public  static String  getFromateM(Date time){
        SimpleDateFormat sdf = new SimpleDateFormat(MIDLE_TIME);
        return sdf.format(time);
    }
    /**
     * 长时间格式化
     * @param time
     * @return
     */
    public  static String  getLongFromate(Date time){
        SimpleDateFormat sdf = new SimpleDateFormat(LONG_TIME);
        return sdf.format(time);
    }
    /**
     * 获取指定时间的分
     * @param date
     * @return
     */
    public static int getMinute(Date date) {
        Calendar   calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }
    /**
     * 获取指定时间的小时
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
    /**
     * 下一天
     * @param date
     * @return
     */
    public static String getSecendDay(Date date) {
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, +1);
        return getFromate(calendar.getTime());
    }

    /**
     * 日期格式
     * @param date
     * @return
     */
    public static String getLongFormatDay(String date)  throws ParseException  {
        SimpleDateFormat simpleDateFormat  =new SimpleDateFormat(LONG_TIME);
        Date beforeDate = simpleDateFormat.parse(date);
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(beforeDate);
        return getLongFromate(calendar.getTime());
    }


    /**
     * 中文日期格式
     * @param date
     * @return
     */
    public static String getShortFormatChs(String date)  throws ParseException  {
        SimpleDateFormat simpleDateFormat  =new SimpleDateFormat(SHORT_TIME);
        Date beforeDate = simpleDateFormat.parse(date);
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(beforeDate);
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_TIME_CHS);
        return sdf.format(calendar.getTime());

    }

    /**
     * 前一天
     * @param date
     * @return
     */
    public static String getBeforeDay(String date)  throws ParseException  {
        SimpleDateFormat simpleDateFormat  =new SimpleDateFormat(SHORT_TIME);
        Date beforeDate = simpleDateFormat.parse(date);
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(beforeDate);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return getFromate(calendar.getTime());
    }


    /**
     * 获取前几天
     * @param date
     * @param order
     * @return
     */
     public static String getBeforeDayByOrder(Date date,int order)  throws ParseException  {
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, order);
        return getFromate(calendar.getTime());
    }


    public static String getBeforeDayByOrderLongFromate(Date date,int order)  throws ParseException  {
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, order);
        return getLongFromate(calendar.getTime());
    }



    /**
     * 获取前几天
     * （）
     * @param date
     * @param order
     * @return
     */
    public static String getBeforeDayByOrderLong(Date date,int order)  throws ParseException  {
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, order);
        return getLongFromate(calendar.getTime());
    }

    /**
     * 短时间格式化
     * @param date
     * @return
     */
    public static String getDay(Date date) {
        return getFromate(date);
    }

    /**
     * 获取指定时间下一天的短格式化
     * @param day
     * @return
     */
    public static String getNextDay(String day) throws Exception {
         SimpleDateFormat simpleDateFormat  =new SimpleDateFormat(SHORT_TIME);
         Date date = simpleDateFormat.parse(day);
          return getSecendDay(date);
    }

    /**
     * 时间中格式化
     * @param day
     * @return
     */
    public static String getMDay(String day) throws Exception {
        SimpleDateFormat simpleDateFormat  =new SimpleDateFormat(MIDLE_TIME);
        Date date = simpleDateFormat.parse(day);
        return simpleDateFormat.format(date);
    }




    /**
     * 时间中格式化
     * @param day
     * @return
     */
    public static String getLongDay(String day) throws Exception {
        SimpleDateFormat simpleDateFormat  =new SimpleDateFormat(LONG_TIME);
        Date date = simpleDateFormat.parse(day);
        return simpleDateFormat.format(date);
    }
    /**
     * 时间短格式化
     * @param day
     * @return
     */
    public static String getDay(String day) throws Exception {
        SimpleDateFormat simpleDateFormat  =new SimpleDateFormat(SHORT_TIME);
        Date date = simpleDateFormat.parse(day);
        return simpleDateFormat.format(date);
    }

    /**
     *  修改指定时间的格式
     * @param date
     * @param field
     * @param amout
     * @return
     */
    public static Date addDate(Date date, int field, int amout) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, amout);
        return calendar.getTime();
    }


}
